<?php
class ModelSurveyAnswer extends Model {
	public function addAnswer($data) {}
	
	public function editAnswer($answer_id, $data) {}
	
	public function deleteAnswer($answer_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "answer` WHERE answer_id = '" . (int)$answer_id . "'");
	}		

	public function getAnswer($answer_id) {
		$sql = "SELECT o.*, CONCAT(c.firstname, ' ', c.lastname) AS customer, o.date_added, o.date_modified FROM `" . DB_PREFIX . "answer` o LEFT JOIN `" . DB_PREFIX . "customer` c ON (o.customer_id = c.customer_id) WHERE answer_id = '" . $answer_id . "' ";
		
		$answer_query = $this->db->query($sql);
		
		if ($answer_query->num_rows) {
			return $answer_query->row;
		} else {
			return false;
		}
	}

	public function getAnswers($data = array()) {
		$sql = "SELECT o.*, CONCAT(c.firstname, ' ', c.lastname) AS customer, o.date_added, o.date_modified FROM `" . DB_PREFIX . "answer` o LEFT JOIN `" . DB_PREFIX . "customer` c ON (o.customer_id = c.customer_id) WHERE 1=1 ";

		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}

		if (isset($data['filter_customer']) && !is_null($data['filter_customer'])) {
			$sql .= " AND CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}
		
		if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
			$sql .= " AND o.email LIKE '%" . $this->db->escape($data['filter_email']) . "%'";
		}
		
		if (isset($data['filter_email_exact']) && !is_null($data['filter_email_exact'])) {
			$sql .= " AND o.email = '" . $this->db->escape($data['filter_email_exact']) . "'";
		}

		if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		$sort_data = array(
			'o.answer_id',
			'o.survey_id',
			'o.question_id',
			'customer',
			'o.email',
			'o.date_added',
			'o.date_modified'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY o.answer_id";
		}

		if (isset($data['answer']) && ($data['answer'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalAnswers($data = array()) {
      	$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "answer` o WHERE 1=1 ";

		if (isset($data['filter_answer_id']) && !is_null($data['filter_answer_id'])) {
			$sql .= " AND o.answer_id = '" . (int)$data['filter_answer_id'] . "'";
		}
		
		if (isset($data['filter_survey_id']) && !is_null($data['filter_survey_id'])) {
			$sql .= " AND o.survey_id = '" . (int)$data['filter_survey_id'] . "'";
		}
		
		if (isset($data['filter_question_id']) && !is_null($data['filter_question_id'])) {
			$sql .= " AND o.question_id = '" . (int)$data['filter_question_id'] . "'";
		}

		if (isset($data['filter_customer']) && !is_null($data['filter_customer'])) {
			$sql .= " AND CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}
		
		if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
			$sql .= " AND o.email LIKE '%" . $this->db->escape($data['filter_email']) . "%'";
		}

		if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}
		
//		echo $sql;
		
		$query = $this->db->query($sql);

		return $query->row['total'];
	}
}
?>